const strFormat = require("../../util/strFormat");
const sql = (str, data) => {
  return strFormat(str, data);
};

const modifylineinfo = (connection, req, res) => {
  const data = req.body.data;
  var sql_str = ["update line set", "where id={id};"];
  Object.keys(data).forEach(item => {
    if ( item !== "id" )
      if ( typeof(data[item]) === "string" )
        sql_str.splice(1, 0, ",", item+"='{"+item+"}'");
      else
        sql_str.splice(1, 0, ",", item+"={"+item+"}");
  });
  sql_str.splice(1,1);

  connection.query(sql(sql_str.join(" "), data), (err, result) => {
    var data = null;

    if ( err || result.affectedRows === 0 ) {
      if ( err ) console.error(err);
      res.status(200).json({
        status: "fail",
        data: data,
      });
      return;
    } else {
      data = Object.assign({}, req.body.data);

      res.status(200).json({
        status: "success",
        data: data,
      });
    }
  });
}

module.exports = modifylineinfo;